/**
 * 
 */
package dms.presentation.action.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import dms.dto.StudentAffairTeacherDTO;
import dms.model.role.wrapper.SystemAdministratorWrapper;
import dms.service.AuthenticationService;
import dms.util.ContextUtil;

/**
 * @author lrz
 *
 */
public class AddTeacherAction extends Action {
	
	private Log log = LogFactory.getLog(AddTeacherAction.class);
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
ActionErrors errors = new ActionErrors();
		
		StudentAffairTeacherDTO studentAffairTeacherDTO = (StudentAffairTeacherDTO) form;
		
		SystemAdministratorWrapper saWrapper = (SystemAdministratorWrapper) ContextUtil.getContext().getBean("systemAdministratorWrapper");
		AuthenticationService authenticationService = (AuthenticationService) ContextUtil.getContext().getBean("authenticationService");
		boolean exist = authenticationService.isUserExist(studentAffairTeacherDTO.getName());
		if (exist) {
			errors.add("errors", new ActionMessage("该用户已存在", false));
			log.warn("用户已存在");
			this.addErrors(request, errors);
			return mapping.getInputForward();
		}
		
		saWrapper.addStudentAffairTeacher(studentAffairTeacherDTO);
		return mapping.findForward("success");
	}
}
